package com.igogogo.model.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.igogogo.model.dao.Ig_JhsMapper;
import com.igogogo.model.entity.Ig_Jhs;
import com.igogogo.model.service.Ig_JhsService;
import com.igogogo.tbkrequest.TbkRequest;
import com.igogogo.utils.TbkUtils;
import com.taobao.api.domain.NTbkItem;
import com.taobao.api.request.TbkItemInfoGetRequest;
import com.taobao.api.request.TbkTpwdCreateRequest;
import com.taobao.api.response.TbkItemInfoGetResponse;
import com.taobao.api.response.TbkSpreadGetResponse;
import com.taobao.api.response.TbkTpwdCreateResponse;

@Service
@Transactional
public class Ig_JhsServiceImpl implements Ig_JhsService {

	@Autowired
	private Ig_JhsMapper ig_JhsMapper;

	@Override
	public int add(Ig_Jhs t) {
		TbkItemInfoGetRequest itemRequest = new TbkItemInfoGetRequest();
		itemRequest.setNumIids(t.getItemId() + "");
		TbkItemInfoGetResponse queryItemInfo = TbkUtils.queryItemInfo(itemRequest);
		if (queryItemInfo.getResults() != null && queryItemInfo.getResults().size() > 0) {
			// 获取小图列表
			NTbkItem nTbkItem = queryItemInfo.getResults().get(0);
			t.setSmallImages(nTbkItem.getSmallImages().toString().replace("[", "").replace("]", ""));
			// 生成短链接
			TbkRequest urls = new TbkRequest();
			urls.setUrls(new String[] { t.getPcUrl() });
			TbkSpreadGetResponse querySpread = TbkUtils.querySpread(urls);
			t.setShortUrl(querySpread.getResults().get(0).getContent());
			// 淘口令
			TbkTpwdCreateRequest tpwdRequest = new TbkTpwdCreateRequest();
			tpwdRequest.setText(t.getTitle());
			tpwdRequest.setUrl(t.getPcUrl());
			tpwdRequest.setLogo(t.getPicUrlForWL());
			TbkTpwdCreateResponse queryTpwdCreate = TbkUtils.queryTpwdCreate(tpwdRequest);
			if (queryTpwdCreate != null) {
				t.setTpwd(queryTpwdCreate.getData().getModel());
			}
		}
		return ig_JhsMapper.insertSelective(t);
	}

	@Override
	public int addmore(List<Ig_Jhs> ts) {
		int result = 0;
		try {
			for (Ig_Jhs ig_Jhs : ts) {
				TbkItemInfoGetRequest itemRequest = new TbkItemInfoGetRequest();
				itemRequest.setNumIids(ig_Jhs.getItemId() + "");
				TbkItemInfoGetResponse queryItemInfo = TbkUtils.queryItemInfo(itemRequest);
				if (queryItemInfo.getResults() != null && queryItemInfo.getResults().size() > 0) {
					// 获取小图列表
					NTbkItem nTbkItem = queryItemInfo.getResults().get(0);
					ig_Jhs.setSmallImages(nTbkItem.getSmallImages().toString().replace("[", "").replace("]", ""));
					// 生成短链接
					TbkRequest urls = new TbkRequest();
					urls.setUrls(new String[] { ig_Jhs.getPcUrl() });
					TbkSpreadGetResponse querySpread = TbkUtils.querySpread(urls);
					ig_Jhs.setShortUrl(querySpread.getResults().get(0).getContent());
					// 淘口令
					TbkTpwdCreateRequest tpwdRequest = new TbkTpwdCreateRequest();
					tpwdRequest.setText(ig_Jhs.getTitle());
					tpwdRequest.setUrl(ig_Jhs.getPcUrl());
					tpwdRequest.setLogo(ig_Jhs.getPicUrlForWL());
					TbkTpwdCreateResponse queryTpwdCreate = TbkUtils.queryTpwdCreate(tpwdRequest);
					if (queryTpwdCreate != null) {
						ig_Jhs.setTpwd(queryTpwdCreate.getData().getModel());
					}
				}
				ig_JhsMapper.insertSelective(ig_Jhs);
			}
			result = 1;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public int delete(Integer id) {
		return ig_JhsMapper.deleteByPrimaryKey(id);
	}

	@Override
	public int update(Ig_Jhs t) {
		return ig_JhsMapper.updateByPrimaryKeySelective(t);
	}

	@Override
	public List<Ig_Jhs> query() {
		return ig_JhsMapper.query();
	}

	@Override
	public Ig_Jhs queryById(Integer id) {
		return ig_JhsMapper.queryById(id);
	}

	@Override
	public List<Ig_Jhs> queryByPage(Map<String, Object> map) {
		return ig_JhsMapper.queryByPage(map);
	}

	@Override
	public List<Ig_Jhs> queryByCondition(Map<String, Object> condition) {
		return ig_JhsMapper.queryByCondition(condition);
	}

	@Override
	public int deleteByMoreId(List<Integer> ids) {
		return ig_JhsMapper.deleteByMoreId(ids);
	}

	@Override
	public int updatemore(List<Ig_Jhs> ts) {
		int result = 0;
		try {
			for (Ig_Jhs ig_Jhs : ts) {
				ig_JhsMapper.updateByPrimaryKeySelective(ig_Jhs);
			}
			result = 1;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public int queryCount() {
		return ig_JhsMapper.queryCount();
	}

	@Override
	public List<Ig_Jhs> queryKeyWordByPage(Map<String, Object> map) {
		return ig_JhsMapper.queryKeyWordByPage(map);
	}

	@Override
	public int queryCountByKeyWord(String keyword) {
		return ig_JhsMapper.queryCountByKeyWord(keyword);
	}

}
